/**
 * 2D坐标类
 */
export class V2 {
    public static ZERO: V2 = new V2(0, 0);

    public x: number = 0;
    public y: number = 0;

    constructor(x?: number, y?: number) {
        this.x = x;
        this.y = y;
    }

    public setXY(x : number, y : number) {
        this.x = x;
        this.y = y;
    }

    public equal(v: V2) : boolean {
        if(v == null) return false;
        return this.x == v.x && this.y == v.y;
    }

    public static Add(v1: V2, v2: V2) : V2 {
        return new V2(v1.x + v2.x, v1.y + v2.y);
    }

    public static Equal(v1:V2,v2:V2):boolean{
        return v1.x == v2.x && v1.y == v2.y;
    }

    public add(v: V2) : V2 {
        this.x += v.x;
        this.y += v.y;
        return this;
    }

    public negative(){
        this.x = -this.x;
        this.y = -this.y;
    }

    public static Sub(v1: V2, v2: V2) : V2 {
        return new V2(v1.x - v2.x, v1.y - v2.y);
    }

    public sub(v: V2) : void {
        this.x -= v.x;
        this.y -= v.y;
    }

    public static Mul(v: V2, f: number) : V2 {
        return new V2(v.x * f, v.y * f);
    }

    public mul(f: number) : V2 {
        this.x *= f;
        this.y *= f;
        return this;
    }

    public mid() : V2 {
        return new V2(this.x / 2, this.y / 2);
    }

    public getLength() : number {
        return Math.sqrt(this.x * this.x + this.y * this.y);
    }

    public getLengthSQ() : number {
        return this.x * this.x + this.y * this.y;
    }

    public normalize() : V2 {
        let length: number = this.getLength();
        if (length == 0) {
            return this;
        }

        this.x = this.x / length;
        this.y = this.y / length;
        return this;
    }

    public dot(v: V2) : number {
        return this.x * v.x + this.y * v.y;
    }

    public toString():string{
        return "("+this.x +","+this.y+")"
    }

    public static CalcDistance(v1: V2, v2: V2) : number {
        return V2.Sub(v1, v2).getLength();
    }

    public static CalcDistanceSQ(v1: V2, v2: V2) : number {
        return V2.Sub(v1, v2).getLengthSQ();
    }
}